// D:\java-project\JavaFx\src\main\java\com\heima\ConnectionLine.java
package com.heima;

import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * 表示两个图形之间的连接线
 */
public class ConnectionLine {
    private ConnectionPoint start;
    private ConnectionPoint end;
    private Color color = Color.BLACK; // 保留原有color字段（可能是填充色）
    private Color lineColor; // 新增：连接线颜色
    private double strokeWidth = 2.0;
    private LineStyle lineStyle = LineStyle.STRAIGHT;

    public ConnectionLine(ConnectionPoint start, ConnectionPoint end) {
        this.start = start;
        this.end = end;
        // 默认颜色为起始图形颜色
        this.lineColor = (start.getOwner() != null) ? start.getOwner().getColor() : Color.BLACK;
    }

    public ConnectionLine(ConnectionPoint start, ConnectionPoint end, LineStyle lineStyle) {
        this.start = start;
        this.end = end;
        // 默认颜色为起始图形颜色
        this.lineColor = (start.getOwner() != null) ? start.getOwner().getColor() : Color.BLACK;
        this.lineStyle = lineStyle;
    }

    public void draw(GraphicsContext gc) {
        gc.setStroke(lineColor); // 使用 lineColor 而不是 Color.BLACK
        gc.setLineWidth(strokeWidth);

        switch (lineStyle) {
            case STRAIGHT:
                gc.strokeLine(start.getPosition().getX(), start.getPosition().getY(),
                        end.getPosition().getX(), end.getPosition().getY());
                break;
            case POLYLINE:
                // 示例：简单直角折线
                double midX = (start.getPosition().getX() + end.getPosition().getX()) / 2;
                gc.strokeLine(start.getPosition().getX(), start.getPosition().getY(), midX, start.getPosition().getY());
                gc.strokeLine(midX, start.getPosition().getY(), midX, end.getPosition().getY());
                gc.strokeLine(midX, end.getPosition().getY(), end.getPosition().getX(), end.getPosition().getY());
                break;
            case CURVE:
                // 示例：贝塞尔曲线（简化）
                double cpX = (start.getPosition().getX() + end.getPosition().getX()) / 2;
                double cpY = (start.getPosition().getY() - end.getPosition().getY()) / 2;
                gc.beginPath();
                gc.moveTo(start.getPosition().getX(), start.getPosition().getY());
                gc.quadraticCurveTo(cpX, cpY, end.getPosition().getX(), end.getPosition().getY());
                gc.stroke();
                break;
        }

        // 绘制箭头
        drawArrow(gc, end.getPosition().getX(), end.getPosition().getY(), start.getPosition().getX(), start.getPosition().getY());
    }

    private void drawArrow(GraphicsContext gc, double endX, double endY, double startX, double startY) {
        // 箭头大小与连接线粗细成比例（最小为2）
        double arrowSize = Math.max(10, strokeWidth * 1.5+10);
        double angle = Math.atan2(endY - startY, endX - startX);
        gc.beginPath();
        gc.moveTo(endX, endY);
        gc.lineTo(endX - arrowSize * Math.cos(angle - Math.PI / 6), endY - arrowSize * Math.sin(angle - Math.PI / 6));
        gc.lineTo(endX - arrowSize * Math.cos(angle + Math.PI / 6), endY - arrowSize * Math.sin(angle + Math.PI / 6));
        gc.closePath();
        // 使用 lineColor 而不是 color 来绘制箭头，避免影响图形组件的边框
        gc.setFill(lineColor);
        gc.fill();
    }

    public void setColor(Color color) {
        this.lineColor = color;
    }

    public Color getLineColor() {
        return lineColor;
    }

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public void setStrokeWidth(double strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    public void setLineStyle(LineStyle style) {
        this.lineStyle = style;
    }

    /**
     * 获取连接线起点
     * @return 连接线起点
     */
    public ConnectionPoint getStart() {
        return start;
    }

    /**
     * 设置连接线起点
     * @param start 新的起点
     */
    public void setStart(ConnectionPoint start) {
        this.start = start;
    }

    /**
     * 获取连接线终点
     * @return 连接线终点
     */
    public ConnectionPoint getEnd() {
        return end;
    }

    /**
     * 设置连接线终点
     * @param end 新的终点
     */
    public void setEnd(ConnectionPoint end) {
        this.end = end;
    }

    public Color getColor() {
        return color;
    }

    public double getStrokeWidth() {
        return strokeWidth;
    }

    public LineStyle getLineStyle() {
        return lineStyle;
    }

    // 新增方法：绘制连接点（如果需要）

    public void drawConnectionPoints(GraphicsContext gc) {
        // 可选：实现连接线端点的可视化表示
        // 这里保持简单，只绘制两个端点
        gc.setFill(Color.BLUE);
        // 起点连接点
        gc.fillOval(start.getPosition().getX() - 5, start.getPosition().getY() - 5, 10, 10);
        // 终点连接点
        gc.fillOval(end.getPosition().getX() - 5, end.getPosition().getY() - 5, 10, 10);
    }

    // 新增方法：获取选中的连接点（如果需要）

    public ConnectionPoint getSelectedConnectionPoint(double mouseX, double mouseY) {
        // 检查鼠标是否点击在起点或终点附近
        Point2D startPoint = start.getPosition();
        Point2D endPoint = end.getPosition();
        
        // 检查起点
        if (Math.abs(mouseX - startPoint.getX()) <= 10 && Math.abs(mouseY - startPoint.getY()) <= 10) {
            return start;
        }
        // 检查终点
        if (Math.abs(mouseX - endPoint.getX()) <= 10 && Math.abs(mouseY - endPoint.getY()) <= 10) {
            return end;
        }
        
        return null;
    }

    public void updatePositions(DrawableShape shape1, DrawableShape shape2) {
        if (shape1 instanceof RectangleShape) {
            RectangleShape rect = (RectangleShape) shape1;
            if (start.getDirection().equals("top")) {
                start.updatePosition(rect.x + rect.width / 2, rect.y);
            } else if (start.getDirection().equals("bottom")) {
                start.updatePosition(rect.x + rect.width / 2, rect.y + rect.height);
            } else if (start.getDirection().equals("left")) {
                start.updatePosition(rect.x, rect.y + rect.height / 2);
            } else if (start.getDirection().equals("right")) {
                start.updatePosition(rect.x + rect.width, rect.y + rect.height / 2);
            }
        } else if (shape1 instanceof DiamondShape) {
            DiamondShape diamond = (DiamondShape) shape1;
            if (start.getDirection().equals("top")) {
                start.updatePosition(diamond.centerX, diamond.centerY - diamond.height / 2);
            } else if (start.getDirection().equals("bottom")) {
                start.updatePosition(diamond.centerX, diamond.centerY + diamond.height / 2);
            } else if (start.getDirection().equals("left")) {
                start.updatePosition(diamond.centerX - diamond.width / 2, diamond.centerY);
            } else if (start.getDirection().equals("right")) {
                start.updatePosition(diamond.centerX + diamond.width / 2, diamond.centerY);
            }
        } else if (shape1 instanceof EllipseShape) {
            EllipseShape ellipse = (EllipseShape) shape1;
            if (start.getDirection().equals("top")) {
                start.updatePosition(ellipse.centerX, ellipse.centerY - ellipse.radiusY);
            } else if (start.getDirection().equals("bottom")) {
                start.updatePosition(ellipse.centerX, ellipse.centerY + ellipse.radiusY);
            } else if (start.getDirection().equals("left")) {
                start.updatePosition(ellipse.centerX - ellipse.radiusX, ellipse.centerY);
            } else if (start.getDirection().equals("right")) {
                start.updatePosition(ellipse.centerX + ellipse.radiusX, ellipse.centerY);
            }
        }

        if (shape2 instanceof RectangleShape) {
            RectangleShape rect = (RectangleShape) shape2;
            if (end.getDirection().equals("top")) {
                end.updatePosition(rect.x + rect.width / 2, rect.y);
            } else if (end.getDirection().equals("bottom")) {
                end.updatePosition(rect.x + rect.width / 2, rect.y + rect.height);
            } else if (end.getDirection().equals("left")) {
                end.updatePosition(rect.x, rect.y + rect.height / 2);
            } else if (end.getDirection().equals("right")) {
                end.updatePosition(rect.x + rect.width, rect.y + rect.height / 2);
            }
        } else if (shape2 instanceof DiamondShape) {
            DiamondShape diamond = (DiamondShape) shape2;
            if (end.getDirection().equals("top")) {
                end.updatePosition(diamond.centerX, diamond.centerY - diamond.height / 2);
            } else if (end.getDirection().equals("bottom")) {
                end.updatePosition(diamond.centerX, diamond.centerY + diamond.height / 2);
            } else if (end.getDirection().equals("left")) {
                end.updatePosition(diamond.centerX - diamond.width / 2, diamond.centerY);
            } else if (end.getDirection().equals("right")) {
                end.updatePosition(diamond.centerX + diamond.width / 2, diamond.centerY);
            }
        } else if (shape2 instanceof EllipseShape) {
            EllipseShape ellipse = (EllipseShape) shape2;
            if (end.getDirection().equals("top")) {
                end.updatePosition(ellipse.centerX, ellipse.centerY - ellipse.radiusY);
            } else if (end.getDirection().equals("bottom")) {
                end.updatePosition(ellipse.centerX, ellipse.centerY + ellipse.radiusY);
            } else if (end.getDirection().equals("left")) {
                end.updatePosition(ellipse.centerX - ellipse.radiusX, ellipse.centerY);
            } else if (end.getDirection().equals("right")) {
                end.updatePosition(ellipse.centerX + ellipse.radiusX, ellipse.centerY);
            }
        }
    }
    
    public void setStyle(LineStyle lineStyle) {
        this.lineStyle = lineStyle;
    }
    
    // 新增方法：更新连接线位置
  


    public Element toXml(Document doc) {
        Element element = doc.createElement("connection");
        
        // 获取起点和终点坐标
        Point2D startPointPos = start.getPosition();
        Point2D endPointPos = end.getPosition();
        
        // 保存连接线基本信息
        element.setAttribute("startX", String.valueOf(startPointPos.getX()));
        element.setAttribute("startY", String.valueOf(startPointPos.getY()));
        element.setAttribute("endX", String.valueOf(endPointPos.getX()));
        element.setAttribute("endY", String.valueOf(endPointPos.getY()));
        element.setAttribute("color", lineColor.toString());
        element.setAttribute("strokeWidth", String.valueOf(strokeWidth));
        element.setAttribute("lineStyle", lineStyle.toString());
        
        // 保存连接点方向信息
        element.setAttribute("startDirection", start.getDirection());
        element.setAttribute("endDirection", end.getDirection());
        
        return element;
    }
}
